home := $(shell echo $$HOME)
pwd  := $(shell pwd)
date := $(shell date +%y%m%d)

dist_file_name := my-dotfile






############################### usr ###############################
usr_origin_files  += /usr/share/color/icc/colord/Sharp_L24.icc
usr_origin_files  += /usr/src/linux/.config

############################### etc ###############################
etc_origin_files  += /etc/fstab
etc_origin_files  += /etc/issue
#etc_origin_files  += /etc/motd
etc_origin_files  += /etc/docker/daemon.json
etc_origin_files  += /etc/grub.d/40_custom
etc_origin_files  += /etc/conf.d/net
#etc_origin_files  += /etc/X11/xorg.conf
etc_origin_files  += $(wildcard /etc/X11/xorg.conf.d/*.conf)
etc_origin_files  += /etc/pulse/default.pa
etc_origin_files  += $(wildcard /etc/udev/rules.d/*.rules)

############################# portage #############################
etc_origin_files  += /etc/portage/make.conf
etc_origin_files  += $(wildcard /etc/portage/package.mask/*.mask)
etc_origin_files  += $(wildcard /etc/portage/package.use/*.use)
etc_origin_files  += $(wildcard /etc/portage/repos.conf/*.conf)

############################# .dotfile ############################
home_origin_files += $(home)/.bashrc
home_origin_files += $(home)/.xinitrc
home_origin_files += $(home)/.Xresources

############################# .config #############################
home_origin_files += $(home)/.config/nvim/init.lua
home_origin_files += $(home)/.config/picom/picom.conf
home_origin_files += $(home)/.config/yt-dlp/config
home_origin_files += $(home)/.config/ncmpcpp/config
home_origin_files += $(home)/.config/ncmpcpp/bindings
home_origin_files += $(home)/.config/gdb/gdbinit
home_origin_files += $(home)/.config/wezterm/wezterm.lua
home_origin_files += $(home)/.config/ranger/rc.conf
home_origin_files += $(home)/.config/redshift/redshift.conf
home_origin_files += $(home)/.config/mpd/mpd.conf
home_origin_files += $(shell find $(home)/.config/alacritty)
home_origin_files += $(shell find $(home)/.config/tmux)
home_origin_files += $(shell find $(home)/.config/feh)
home_origin_files += $(shell find $(home)/.config/mpv)

############################## Scripts ############################
home_origin_files += $(shell find $(home)/Scripts)

############################## Docker #############################
home_origin_files += $(shell find $(home)/Docker -maxdepth 2)
###################################################################








home_backup_files := $(subst $(home),$(pwd)/home,$(home_origin_files))
etc_backup_files  := $(subst /etc/,$(pwd)/etc/,$(etc_origin_files))
usr_backup_files  := $(subst /usr/,$(pwd)/usr/,$(usr_origin_files))

new_backup_files  := $(home_backup_files) $(etc_backup_files) $(usr_backup_files)
old_backup_files  := $(shell find $(pwd)/usr $(pwd)/etc $(pwd)/home)

deprecate_backup_files := $(filter-out $(new_backup_files),$(old_backup_files))



# origin:$(1)   backup:$(2)
define copy
	@if [ -f $(1) ];then \
	test -d `dirname $(2)` || mkdir -p `dirname $(2)`; \
	cp $(1) $(2) && echo -e "[\e[1;32m copy \e[0m]-[\e[1;33m $(1) \e[0m]->[\e[1;36m $(2) \e[0m]"; \
	fi
endef


backup: $(new_backup_files) rm-deprecate
	@echo -e "[\e[1;32m done \e[0m]"


$(filter $(pwd)/home/%,$(new_backup_files)): $(pwd)/home/%: $(home)/%
	$(call copy,$<,$@)


$(filter $(pwd)/etc/%,$(new_backup_files)): $(pwd)/etc/%: /etc/%
	$(call copy,$<,$@)


$(filter $(pwd)/usr/%,$(new_backup_files)): $(pwd)/usr/%: /usr/%
	$(call copy,$<,$@)


dist:
	@cd .. && tar cf /tmp/$(dist_file_name)-$(date).tar.gz --exclude="*.tar.gz" `basename $(pwd)`
	@cp /tmp/$(dist_file_name)-$(date).tar.gz .


rm-deprecate:
	@for var in $(deprecate_backup_files); do \
	origin=`echo $$var | sed "s#$(pwd)/home#$(home)#" | sed "s#$(pwd)/etc#/etc#" | sed "s#$(pwd)/usr#/usr#"`; \
	if [ ! -f $$origin ] && [ ! -d $$origin ];then \
	echo -e "[\e[1;31m deprecate \e[0m]-[\e[1;36m $$var \e[0m]"; \
	rm -i -r $$var; \
	fi; \
	done


git:
	git add -A
	git commit -m "update"
	git push
